package com.zhkj.txg.module.home.ui

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayoutMediator
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.module.home.adapter.HomeBannerAdapter
import com.zhkj.txg.module.home.entity.BannerEntity
import com.zhkj.txg.module.home.vm.MallCategoryViewModel
import com.zhkj.txg.utils.typeJump
import kotlinx.android.synthetic.main.activity_mall_category.*

class MallCategoryActivity : ViewModelActivity<MallCategoryViewModel>() {
    override fun statusTitleBar(): View? = topViewMallCategory
    override fun setViewModel(): MallCategoryViewModel = createViewModel()
    override val layoutResId: Int = R.layout.activity_mall_category

    companion object {

        private val TITLE = "title"
        private val ID = "catId"

        fun open(context: Context, title: String, id: Int) {
            val intent = Intent(context, MallCategoryActivity::class.java)
                .putExtra(TITLE, title).putExtra(ID, id)
            context.startActivity(intent)
        }
    }

    override fun initUI() {
        tvMallCategorySearch.setOnClickListener {
            SearchActivity.mallOpen(this, viewModel.cateId.toLong())
        }
        with(vpMallCategory) {
            getChildAt(0)?.overScrollMode = View.OVER_SCROLL_NEVER
            orientation = ViewPager2.ORIENTATION_HORIZONTAL
            offscreenPageLimit = 1
            adapter = object : FragmentStateAdapter(this@MallCategoryActivity) {
                override fun getItemCount(): Int = 2
                override fun createFragment(position: Int): Fragment = when (position) {
                    1 -> MallCategoryBrandFragment.getInstance(intent.getIntExtra(ID, 0))
                    else -> MallCategoryProductFragment.getInstance(intent.getIntExtra(ID, 0))
                }
            }
        }
        bannerMallCategory.addBannerLifecycleObserver(this).setAdapter(HomeBannerAdapter(this))
            .setOnBannerListener { data, _ ->
                (data as? BannerEntity)?.let {
                    typeJump(this, it.jumpType ?: 0, it.params, it.title)
                }
            }
        TabLayoutMediator(tabMallCategory, vpMallCategory,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                run {
                    when (position) {
                        1 -> tab.setCustomView(R.layout.item_mall_cate_brand)
                        else -> tab.setCustomView(R.layout.item_mall_cate_product)
                    }
                }
            }).attach()
        // 打开购物车
        ivBasket.setOnClickListener {
            startActivity(Intent(this, CartActivity::class.java))
        }
    }

    override fun initViewModel() {
        viewModel.shopListLiveData.observe(this, Observer { response ->
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let {
                bannerMallCategory.adapter.setDatas(it.banner)
                bannerMallCategory.adapter.notifyDataSetChanged()
                bannerMallCategory.start()
            }
        })
    }

    override fun initData() {
        viewModel.cateId = intent.getIntExtra(ID, 0)
        viewModel.mallCategory(AccountManager.getToken(), 0, true)
    }
}